package com.itheima.controller.backend.security.order;


import com.aliyun.oss.common.utils.HttpHeaders;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.common.utils.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    OrderSettingService orderSettingService;
    @Autowired
    AliOssUtil aliOssUtil;


    @GetMapping("/download")
    public String download(@RequestParam String filename,HttpServletResponse response) throws IOException {


        new POIUtils().downloadExcel(response,"ordersetting_template.xlsx", "订单设置导入模板.xlsx");
        return "下载成功";
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editMNumberByDate(@RequestBody OrderSetting orderSetting) {

        orderSettingService.editMNumberByDate(orderSetting);
        return new Result(true, "修改成功");
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam String month) {

        List<OrderSettingVO> orderSetting = orderSettingService.getOrderSettingByMonth(month);

        return new Result(true, "查询成功", orderSetting);
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {


        List<String[]> ExcelMessage = POIUtils.readExcel(excelFile);
        orderSettingService.add(ExcelMessage);

//        String originalFilename = excelFile.getOriginalFilename();
//        // 获取文件后缀名
//        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String filename = UUID.randomUUID().toString()+ fileExtension;
//        String URL = aliOssUtil.upload(excelFile.getBytes(), filename);
        return new Result(true , "上传成功");

    }


}